unit ItensPedido;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, DBGrids, DB, ExtCtrls, Funcoes, DmVariaveis;

type
  TFrmItensPedido = class(TForm)
    BitImprimirCupom: TBitBtn;
    BitCancelarImpressao: TBitBtn;
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    LblRedBase: TLabel;
    EdtRedBase: TEdit;
    Ds_ItensPedido: TDataSource;
    Label2: TLabel;
    St_TotPedido: TStaticText;
    Label3: TLabel;
    St_TotCupom: TStaticText;
    Bevel1: TBevel;
    procedure EdtRedBaseKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure EdtRedBaseExit(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmItensPedido: TFrmItensPedido;

implementation

uses DMCaixa;

{$R *.dfm}

procedure TFrmItensPedido.EdtRedBaseKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not ENumerico(key) then key := #0;
  if key = '.'          then key := ',';
end;

procedure TFrmItensPedido.FormShow(Sender: TObject);
Var
  TotPedido : Currency;
begin
  Ds_ItensPedido.DataSet := DmCaixas.SQLC_ItensPedido;
  TotPedido := 0;
  if Ds_ItensPedido.DataSet.Active then begin
    with Ds_ItensPedido.DataSet do begin
      First;
      while not eof do begin
        Edit;
        FieldbyName('Total').AsFloat := FieldbyName('PREVEN').AsFloat *
                                        FieldbyName('QUANTIDADE').AsFloat;
        FieldbyName('CXS_PRETAB').AsFloat := FieldbyName('PREVEN').AsFloat;
        TotPedido := TotPedido + FieldbyName('Total').AsFloat;
        Next;
      end;
    end;
  end;
  St_TotPedido.Caption := ArredondaStr( TotPedido, '1' );
  St_TotCupom.Caption  := ArredondaStr( TotPedido, '1' );
  EdtRedBase.Visible := Pc_ReduBase='S';
  LblRedBase.Visible := Pc_ReduBase='S';
  BitImprimirCupom.SetFocus;
end;

procedure TFrmItensPedido.EdtRedBaseExit(Sender: TObject);
Var
  TotCupom    : Currency;
  ReducaoBase : Currency;
begin
  ReducaoBase := StrToFloatDef( EdtRedBase.Text, 0 );
  if ReducaoBase > 0 then
     St_TotCupom.Color := $00CDDCDB
  else
     St_TotCupom.Color := $00FFFF80;

  if Ds_ItensPedido.DataSet.Active then begin
    with Ds_ItensPedido.DataSet do begin
      First;
      TotCupom := 0;
      while not eof do begin
        Edit;
        if ReducaoBase > 0 then
          FieldbyName('PREVEN').AsFloat :=
              ArredondaReal(FieldbyName('CXS_PRETAB').AsFloat -
                    (FieldbyName('CXS_PRETAB').AsFloat * (ReducaoBase /100)), '1')
        else
          FieldbyName('PREVEN').AsFloat := FieldbyName('CXS_PRETAB').AsFloat;

        if FieldbyName('PREVEN').AsFloat = 0 then
          FieldbyName('PREVEN').AsFloat := 0.01;

        FieldbyName('Total').AsFloat := FieldbyName('PREVEN').AsFloat *
                                        FieldbyName('QUANTIDADE').AsFloat;
        Post;
        TotCupom  := TotCupom + FieldbyName('Total').AsFloat;
        Next;
      end;
      St_TotCupom.Caption := ArredondaStr( TotCupom, '1');
    end;
  end;  
end;

procedure TFrmItensPedido.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then begin
    key:=#0;
    perform(WM_NextDLGCtl,0,0);
  end;
end;

end.
